<?php
header( 'Content-Type: text/html; charset=utf-8');
@session_start();
require_once('../config/config.php');
require_once('../utils/CommonUtils.php');
require_once('../utils/DBUtils.php');
include ("../utils/Constants.php");
require_once('../utils/BaseDAO.php');
require("../utils/Session.php");

$commonUtils = new CommonUtils();
$baseDAO = new BaseDAO();
$loginId = Session::get('userid');

$imageId = $commonUtils->getParameter("id");
$path = $commonUtils->getParameter("p");
$createddate = $commonUtils->getDate();
$status = $loginId != null ? 1 : 0; 
if($imageId != null && $imageId != ""){
	$image = $baseDAO->getItem("image", array("id", "image", "title", "status", "createdby", "createddate", "views", "width", "height"), $imageId);
	$views = $image['views'] + 1;
	$baseDAO->update("image", array("id", "views"), array($imageId, $views));
	
	$comments = $baseDAO->getItems("imagecomment", array("id", "content", "createddate", "createdby"), "status = 1 and imageid = " . $imageId, "order by createddate desc");
}

?>
<html>
	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
	</head>
	<body>
		<div class="imgDetail">
			<div id="imageArea"
				style="<?php if($image['width'] != null && $image['width'] > 0){echo 'width:'.($image['width'] + 6).'px';} ?>
				<?php if($image['height'] != null && $image['height'] > 0){echo ';height:'.($image['height'] + 40).'px';} ?>"
				>
				<div id="imageimage">
					<img src="<?php if(isset($path) && $path != ""){echo $path.'/';} ?>images/images/<?php echo $image['image'];?>"/>
				</div>
				<div style="clear:both;">
					<div class="title" style="float:left;"><?php echo $image['title'];?></div>
					<div style="margin:4px;float:right;">		
						<span class="createdby"><?php echo $image['createdby'];?></span>
						<span class="displayDate"><?php echo $commonUtils->timePost($image['createddate']);?></span>
					</div>
				</div>
			</div>
			<div id="imagecomment" style="<?php if($image['width'] != null && $image['width'] > 0){echo 'width:'.(970 - $image['width']).'px';} ?>">
				<div class="fb-like" data-href="http://www.nguyendau.com/hinh-anh/chi-tiet.html?id=<?php echo $imageId;?>" data-send="false" data-layout="button_count" data-width="<?php if($image['width'] != null && $image['width'] > 0){echo (970 - $image['width']);} ?>" data-show-faces="false" ></div>
				<div class="fb-comments" data-width="<?php if($image['width'] != null && $image['width'] > 0){echo (970 - $image['width']);} ?>" href="http://www.nguyendau.com/hinh-anh/chi-tiet.html?id=<?php echo $imageId;?>" data-num-posts="10"></div>
				<?php /*?>
				<div id="txtInput">
					<input type="text" id="txtCommentImage" name="content" title="Viết bình luận..." width="400px"/>
				</div>
				<div id="tbComments" style="<?php if($image['height'] != null && $image['height'] > 0){echo 'height:'.($image['height'] - 10).'px';} ?>">
					<?php foreach($comments as $comment){?>
					<div class="comment">
						<span id="imgauthor"><?php echo $comment['createdby'] == null ? 'Không tên' : $comment['createdby'];?></span>
						<span id="imgdate" class="displayDate">&nbsp;<?php echo $commonUtils->timePost($comment['createddate']);?></span>
						<?php if(isset($loginId) && $comment['createdby_id'] == $loginId){?>
							<span id="imgdel"><a onclick="delcomment(<?php echo $comment['id'];?>);"><img src="<?php if(isset($path) && $path != ""){echo $path.'/';} ?>css/images/delete.png"/></a></span>
						<?php }?>
						<div style="clear:both;"></div>
						<span id="imgcomment"><?php echo $comment['content'];?></span>
						<div style="clear:both;"></div>
					</div>
					<?php }?>
				</div>
				<?php */?>
			</div>
		</div>
		<div style="clear:both"></div>
		<script type="text/javascript">
		$(function(){
			$("#txtCommentImage").keypress(function(e){
				if (e.which == 13){
					var cm = $("#txtCommentImage").val();
					if(cm != null && cm != ""){
						if('<?php echo Session::get('fullname');?>' == ''){
							if(confirm('Vì bạn chưa đăng nhập nên bình luận sẻ được tác giả phê duyệt trước khi công khai')){
								$.fancybox.showLoading();
								$.ajax({
									url: '<?php if(isset($path) && $path != ""){echo $path."/";} ?>ajax/imagecomment.php?id='+<?php echo $image['id'];?>+'&cm=' + cm,
									success:function(data){
										$("#txtCommentImage").val("");
										alert("Bình luận của bạn đã gửi thành công, bình luận sẻ được hiển thị khi được tác giả phê duyệt");
										$.fancybox.hideLoading();
									}
								});	
							}
						}else{
							$.fancybox.showLoading();
							$.ajax({
								url: '<?php if(isset($path) && $path != ""){echo $path."/";} ?>ajax/imagecomment.php?id='+<?php echo $image['id'];?>+'&cm=' + cm,
								success:function(data){
									$("#txtCommentImage").val("");
									loadComments('<?php echo $image['id'];?>');				
									$.fancybox.hideLoading();
								}
							});	
						}
					}
				}
			});
			
			$(".comment").hover(function(){
				$(this).addClass("comment_");
			}, function(){
				$(this).removeClass("comment_");
			});
			FB.XFBML.parse();
		});
		function loadComments(imageid){
			$.ajax({
				url: '<?php if(isset($path) && $path != ""){echo $path."/";} ?>ajax/loadimagecomment.php?id='+imageid+'<?php if(isset($path) && $path != ""){echo '&p='.$path;} ?>',
				dataType: 'html',
				success:function(html){
					$("#tbComments").html(html);
					$(".comment").hover(function(){
						$(this).addClass("comment_");
					}, function(){
						$(this).removeClass("comment_");
					});
				}
			});	
		}
		function delcomment(commentid){
			if(commentid != null && commentid != ''){
				if(confirm('Bạn có chắc chắn khi xóa bình luận này?')){
					$.fancybox.showLoading();
					$.ajax({
						url: '<?php if(isset($path) && $path != ""){echo $path."/";} ?>ajax/delimagecomment.php?id='+commentid,
						success:function(data){
							loadComments('<?php echo $image['id'];?>');
							$.fancybox.hideLoading();
						}
					});	
				}
			}
		}
		</script>
	</body>
</html>